package net.oschina.victor.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

import org.hibernate.validator.constraints.NotEmpty;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 系统资源信息
 * 
 * @author Victor
 *
 */
@Entity
public class Resource {
	public static enum ResourceType { // 资源类型
		MENU("菜单"), BUTTON("按钮");
		private String info;

		private ResourceType(String info) {
			this.info = info;
		}

		public String getInfo() {
			return info;
		}
	}

	@Id
	@GeneratedValue
	private Long id; // 资源ID
	@NotEmpty(message = "资源名称不能为空")
	private String name; // 资源名称
	private ResourceType type = ResourceType.MENU; // 资源类型
	private String url; // 资源路径
	private int priority; // 显示顺序
	private Long parentId; // 父编号
	private String parentIds;// 父编号列表
	private String permission; // 权限字符串
	private Boolean available = Boolean.FALSE;// 是否可用
	@JsonIgnore
	@ManyToMany(mappedBy = "resources")
	private Set<Role> roles = new HashSet<Role>();// 资源对应角色列表

	/**
	 * 是否为菜单根节点
	 * 
	 * @return
	 */
	public boolean isRootNode() {
		return parentId == 0;
	}

	public Boolean getAvailable() {
		return available;
	}

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public Long getParentId() {
		return parentId;
	}

	public String getParentIds() {
		return parentIds;
	}

	public String getPermission() {
		return permission;
	}

	public int getPriority() {
		return priority;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public ResourceType getType() {
		return type;
	}

	public String getUrl() {
		return url;
	}

	public void setAvailable(Boolean available) {
		this.available = available;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public void setParentIds(String parentIds) {
		this.parentIds = parentIds;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public void setType(ResourceType type) {
		this.type = type;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public String toString() {
		return "Resource [id=" + id + ", name=" + name + ", type=" + type + ", url=" + url + ", priority=" + priority
				+ ", parentId=" + parentId + ", parentIds=" + parentIds + ", permission=" + permission + ", available="
				+ available + "]";
	}

}
